<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="/include/commonHeader::commonHeader('过夜机场')"></head>
<body class="gray-bg layer-blue">
<div class="wrapper wrapper-content  animated fadeInRight full-container">
    <div class="row">
        <div class="ibox float-e-margins">
            <div class="ibox-content">
                <div class="btn-group table-tool" id="exampleToolbar" role="group">
                    <input id="query_airport" type="text" class="table-tool-btn pull-left"
                           placeholder="机场四码" maxlength="4"/>
                    <button type="button" class="btn btn-outline btn-success btn-refresh" data-toggle="tooltip"
                            title="查询" id="btn-search">
                        <i class="fa fa-search" aria-hidden="true"></i>
                    </button>
                    <shiro:hasPermission1 name="basicinfo:overnightAirport:add">
                        <button type="button" onclick="add()" class="btn btn-outline btn-success"
                                data-toggle="tooltip" title="添加">
                            <i class="fa fa-plus" aria-hidden="true"></i>
                        </button>
                    </shiro:hasPermission1>
                    <button type="button" onclick="reload()" class="btn btn-outline btn-success "
                            data-toggle="tooltip" title="刷新">
                        <i class="fa fa-refresh" aria-hidden="true"></i>
                    </button>
                </div>
                <table id="exampleTable" data-classes="table table-hover table-condensed">
                </table>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    var p_delete = 'hidden';
    var p_edit = 'hidden';
</script>
<shiro:hasPermission1 name="basicinfo:overnightAirport:delete">
    <script type="text/javascript">
        p_delete = '';
    </script>
</shiro:hasPermission1>
<shiro:hasPermission1 name="basicinfo:overnightAirport:edit">
    <script type="text/javascript">
        p_edit = '';
    </script>
</shiro:hasPermission1>

<script src="/plugins/bootstrapValidator/js/bootstrapValidator.min.js"></script>
<script src="/plugins/bootstrapValidator/js/language/zh_CN.js"></script>

<!-- 自定义js -->
<script th:inline="javascript">
    var prefix = "/overnightAirport";
    var moduleFlag = [[${moduleFlag}]];

    $(function () {
        load();
        $("#btn-search").click(function(){
            $("#exampleTable").bootstrapTable('refresh');
        });
    });

    function load() {
        $('#exampleTable').bootstrapTable(
            {
                method: 'post',
                url: prefix + "/list.do",
                striped: true,
                dataType: "json",
                pagination: true,
                clickToSelect: true,
                pageSize: 10,
                pageNumber: 1,
                showColumns: false,
                sidePagination: "server",
                paginationLoop: false,
                contentType: "application/x-www-form-urlencoded",
                queryParams: function (params) {
                    return {
                        limit: params.limit,
                        offset: params.offset,
                        nightAirport: $("#query_airport").val().toUpperCase()
                    };
                },
                onDblClickRow: function (row, $element, field) {
                    /*view(row.nightAirport);*/
                },
                columns: [
                    {
                        field: 'idx',
                        title: '序号',
                        formatter: orderFormatter,
                        valign: 'middle',
                        align: 'center',
                        width: '40'
                    },
                    {
                        field: 'nightAirport',
                        title: '过夜机场'
                    },
                    {
                        field: 'isCrew',
                        title: '飞行过夜机场',
                        formatter: YNFormatter
                    },
                    {
                        field: 'isStew',
                        title: '乘务过夜机场',
                        formatter: YNFormatter
                    },
                    {
                        field: 'isPolice',
                        title: '空警过夜机场',
                        formatter: YNFormatter
                    },
                    {
                        field: 'updateByName',
                        title: '更新人'
                    },
                    {
                        field: 'updateTime',
                        title: '更新时间'
                    },
                    {
                        title: '操作',
                        align: 'center',
                        visible: isShowOperation(),
                        formatter: function (value, row, index) {
                            return [
                                '<a class="' + p_edit + '" href="#" title="点击切换是否为过夜机场" onclick=toggle("' + row.nightAirport + '")>',
                                '<i class="glyphicon glyphicon-retweet"></i>',
                                '</a>&nbsp;&nbsp;',
                                '<a class="' + p_delete + '" href="#" title="删除（只能在所有部门都没有使用该过夜机场才可以删除）" onclick=remove("' + row.nightAirport + '")>',
                                '<i class="glyphicon glyphicon-remove text-danger"></i>',
                                '</a>&nbsp;&nbsp;'
                            ].join('');
                        }
                    }]
            })
    }

    //是否有权限显示 '操作' 列
    function isShowOperation() {
        return (p_edit != 'hidden' || p_delete != 'hidden');
    }

    //刷新
    function reload() {
        $("#query_airport").val("");
        $("#exampleTable").bootstrapTable('refresh');
    }

    //增加
    function add() {
        layer.open({
            type: 2,
            title: '新增',
            shadeClose: false, // 点击遮罩关闭层
            area: ['500px', '400px'],
            content: prefix + '/add?moduleFlag=' + moduleFlag
        });
    }

    //编辑
    function edit(nightAirport) {
        layer.open({
            type: 2,
            title: '编辑',
            maxmin: true,
            shadeClose: false, // 点击遮罩关闭层
            area: ['800px', '520px'],
            content: prefix + '/edit?nightAirport=' + nightAirport
        });
    }

    //查看
    function view(nightAirport) {
        layer.open({
            type: 2,
            title: '查看',
            shadeClose: false, // 点击遮罩关闭层
            area: ['600px', '400px'],
            content: prefix + '/view?nightAirport=' + nightAirport
        });
    }

    //删除
    function remove(nightAirport) {
        layer.confirm('确定要删除选中的记录？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/delete",
                type: "post",
                data: {
                    'nightAirport': nightAirport
                },
                success: function (r) {
                    layer.close(index);
                    if (r.code == 0) {
                        toastr.success(r.msg);
                        reload();
                    } else {
                        toastr.warning(r.msg);
                    }
                },
                error:function(){
                    toastr.error('数据出错！');
                    layer.close(index);
                }
            });
        })
    }

    //切换是否为过夜机场
    function toggle(nightAirport) {
        layer.confirm('确定要切换选中的记录？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/toggle.do",
                type: "post",
                data: {
                    'nightAirport': nightAirport,
                    'moduleFlag': moduleFlag
                },
                success: function (r) {
                    layer.close(index);
                    if (r.code == 0) {
                        toastr.success(r.msg);
                        reload();
                    } else {
                        toastr.warning(r.msg);
                    }
                },
                error:function(){
                    toastr.error('数据出错！');
                    layer.close(index);
                }
            });
        })
    }

    function YNFormatter(value, row, index) {
        if (value == 'Y') {
            return '<span class=\"glyphicon glyphicon-ok\"></span>';
        } else {
            return '';
        }
    }

</script>
</body>
</html>